﻿@using Janus.Mes.WebClient;
@using Janus.BaseEntitys;
@using Janus.Mes.VO;
@using Janus.Cores.Tools;
@model RolersVO

@{
    Layout = null;
    var Modules = ViewData["Modules"] as List<ModuleDTO>;
    var CurentUser = Session["CurentUser"] as CurrentUserDTO;
    var i = 1;
    var rolesPrivStr = new List<string>(Model.SelectedModules ?? new List<string>());
    var parentModels = Modules.Where(c => string.IsNullOrEmpty(c.Parent)).OrderBy(c => c.Order);
    string code = ViewData["code"] as string;

    string selectedvalue = Model.Id.IsGuid() ? code : Model.ParentID;
}
<div class="pageContent">
    <style>
        .tbleFirstcat {
            background-color: #999;
            font-weight: bold;
            color: #fff;
        }

            .tbleFirstcat:hover {
                color: #000;
            }

        .tbleSecondcat {
            background-color: #ccc;
            font-weight: bold;
            color: #fff;
        }

            .tbleSecondcat:hover {
                color: #000;
            }

        #treeRoles td {
            padding-right: 2px;
        }
    </style>
    <form method="post" action="System/Rolers/create?OpID=@ViewData["OperateID"]" class="pageForm required-validate"
          onsubmit="return validateCallback(this, dialogAjaxDone);">
        @Html.ValidationSummary(true)
        <div class="pageFormContent" layouth="56">
            @Html.HiddenFor(model => model.Id)
            @*<input id="SelectedRoles" name="SelectedRoles" type="hidden" value="@(string.Join(",",rolesPrivStr))" />*@
            @if (Model.Id.IsGuid())
            {
                <p>
                    <label>
                        编号：
                    </label>
                    @Html.TextBoxFor(model => model.Code, new { @Readonly = "true", @class = "required" })
                    @Html.ValidationMessageFor(model => model.Code)
                </p>
            }
            <p>
                <label>
                    角色名称：
                </label>
                @Html.TextBoxFor(model => model.Name, new { @class = "required" })
                @Html.ValidationMessageFor(model => model.Name)
            </p>
            <p>
                <label>
                    上司：
                </label>
                @Html.SelectRolers(new { @class = "treetable select3", @name = "ParentID", id = "ParentID", selectedvalue = selectedvalue })
                @Html.ValidationMessageFor(model => model.ParentID)
            </p>
            <p>
                <label>
                    级别：
                </label>
                @Html.TextBoxFor(model => model.Level, new { @class = "required digits" })
                @Html.ValidationMessageFor(model => model.Level)
            </p>
            @Html.Partial("Operation", Model)
            <div class="divider">
            </div>
            <div style="width:100%">
                @foreach (var item in parentModels)
                {
                    var isItemSelected = rolesPrivStr.Contains(item.ID);
                    var subs = Modules.Where(c => c.Parent == item.ID).OrderBy(c => c.Order).ToList();
                    string currentId = item.ID;

                    <div style="width:300px; float:left; margin-right:80px;">
                        <div class="panel collapse" minh="350" defh="350">
                            <h1>@item.ModuleName</h1>
                            <div>
                                <table id="treeRoles" class="treetable list" width="100%">
                                    <tr id="@item.ID" pid="@item.Parent" data-tt-id="@item.ID" data-tt-parent-id="@item.Parent">
                                        <td style="border: 1px solid #bcc9cf; ">
                                            <input id="ckb@(item.ID)" type="checkbox" value="@item.ID" name="SelectedModules" rel="@(currentId)" checked="@isItemSelected" onclick="SelectingModules(this)" />
                                            <input type="button" value="单选" style="margin-left:5px;width:32px;height:18px;padding-left:1px;" onclick="singled('ckb@(item.ID)')" />
                                        </td>
                                        <td style="border: 1px solid #bcc9cf; ">@item.ModuleName</td>
                                    </tr>
                                    @GenerateRoles(subs, Modules, rolesPrivStr, currentId)
                                </table>

                            </div>
                        </div>

                    </div>
                }
            </div>
        </div>
        @Html.Partial("FormButtonBar")
    </form>
    <script>
        function SelectingModules(obj) {
            var $this = $(obj);
            var isChecked = obj.checked;
            var $value = $this.val();
            //选择父级后，全选择子级
            $("input[rel*='" + $value + "']", $.pdialog.getCurrent()).attr("checked", isChecked);
            //选择某子级，同时应选择其父级
            var tmpValue = $this.attr("rel");
            var parentValue = "";
            while (tmpValue.indexOf('-') >= 0) {
                var index = tmpValue.indexOf('-');
                var pValue = tmpValue.substring(0, index);
                parentValue = parentValue.length == 0 ? pValue : parentValue + "-" + pValue;
                //当取消选中时，若同级还有选中的，则父级还是选中
                var checked = isChecked ? isChecked : $("input[rel*='" + parentValue + "']:checked", $.pdialog.getCurrent()).length > 0;
                $("input[rel='" + parentValue + "']", $.pdialog.getCurrent()).attr("checked", checked);
                tmpValue = tmpValue.substring((index + 1));
            }
            //var slectedValue = "";
            //$("input:checked", $.pdialog.getCurrent()).each(function (item) {
            //    slectedValue += $(this).val() + ",";
            //});
            //$("#SelectedRoles", $.pdialog.getCurrent()).val(slectedValue);
        }

        function singled(obj) {
            $target = $("#" + obj);
            //$target.attr("checked", !($target.attr("checked")));
            if ($target.attr("checked")) {
                $target.removeAttr("checked");
                $target.attr("checked", false);
            }
            else {
                $target.attr("checked", true);
            }
            var slectedValue = "";
            $("input:checked", $.pdialog.getCurrent()).each(function (item) {
                slectedValue += $(this).val() + ",";
            });
            //$("#SelectedRoles", $.pdialog.getCurrent()).val(slectedValue);

        }
    </script>
</div>
@helper GenerateRoles(List<ModuleDTO> SubModels, List<ModuleDTO> sourceModels, List<string> rolesPrivStr, string panrentId, int deep = 1)
{
foreach (var item in SubModels)
{
    var isItemSelected = rolesPrivStr.Contains(item.ID);//.IndexOf("," + item.ID + ",") > -1;
    var subs = sourceModels.Where(c => c.Parent == item.ID).OrderBy(c => c.Order).ToList();
    string currentId = panrentId + "-" + item.ID;
    var hassubcat = subs.Count > 0;
        <tr id="@item.ID" pid="@item.Parent" data-tt-id="@item.ID" data-tt-parent-id="@item.Parent">
            <td style="border: 1px solid #bcc9cf; ">
                @*padding-left:@(deep * 10)px*@
                <input type="checkbox" id="ckb@(item.ID)" value="@item.ID" name="SelectedModules" rel="@(currentId)" checked="@isItemSelected" onclick="SelectingModules(this)" />
                <input type="button" value="单选" style="margin-left:5px;width:32px;height:18px;padding-left:1px;" onclick="singled('ckb@(item.ID)')" />
            </td>
            <td style="border: 1px solid #bcc9cf">@item.ModuleName</td>
        </tr>
    if (subs.Count > 0)
    {
            @GenerateRoles(subs, sourceModels, rolesPrivStr, currentId, deep + 1)
    }
}
}